package org.yx.service.smat.save_production_manufacture_alternate_material;

import java.util.ArrayList;
import java.util.List;

import org.dbist.dml.Query;
import org.yx.exception.BizException;
import org.yx.models.SbasMatDef;
import org.yx.models.SmatBomAlt;
import org.yx.models.SmatBomDat;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 保存生产BOM的替代物料
 * @author zxw
 * @since 2018/04/16
**/
public class SmatSaveProductionManufactureAlternateMaterialService extends BaseService {
	public SmatSaveProductionManufactureAlternateMaterialOut saveProductionManufactureAlternateMaterial(
			SmatSaveProductionManufactureAlternateMaterialIn in) throws Exception {
		if (CollUtil.isEmpty(in.getAltList())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "altList"));
		}

		//提取原BOM清单
		String prodCode = in.getAltList().get(0).getProdCode(), prodVer = in.getAltList().get(0).getProdVer();
		if (StrUtil.isBlank(prodCode)) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "altList.prodCode"));
		}
		if (StrUtil.isBlank(prodVer)) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "altList.prodVer"));
		}

		Query query = new Query();
		query.addFilter("orgCode", in.getUserOrgCode());
		query.addFilter("prodCode", prodCode);
		query.addFilter("prodVer", prodVer);
		List<SmatBomAlt> oldAltList = selectList(SmatBomAlt.class, query);
		query.addFilter("status", "<>", 'D');
		List<SmatBomDat> bomList = selectList(SmatBomDat.class, query);
		if (CollUtil.isEmpty(bomList)) {
			throw new BizException("SMAT-0001", new FieldMap("prodCode", prodCode));
		}

		List<SmatBomDat> upBomList = new ArrayList<SmatBomDat>();
		for (int i = 0; i < in.getAltList().size(); i++) {
			SmatBomAlt alt = in.getAltList().get(i);
			alt.setOrgCode(in.getUserOrgCode());
			alt.setSeqNum(i + 1);
			if (!StrUtil.equals(prodCode, alt.getProdCode()) || !StrUtil.equals(prodVer, alt.getProdVer())) {
				throw new BizException("SMAT-0002");//一次只能保存一种BOM替代料，不能混BOM
			}

			checkStringNotBlank(alt, "matCode,matVer,mainMatCode,mainMatVer");
			if (alt.getQty() == null || alt.getQty().compareTo(0d) < 0) {
				throw new BizException("SYS-0005", new FieldMap("parameter", "qty"), new FieldMap("value", alt.getQty()));
			}
			//校验替代料存在
			SbasMatDef altMat = selectWithPk(SbasMatDef.class, in.getUserOrgCode(), alt.getMatCode(), alt.getMatVer());
			if (altMat == null || altMat.getMatStatus() == 'D') {
				throw new BizException("SBAS-0002", new FieldMap("matCode", alt.getMatCode()));
			}

			SmatBomDat bomDat = null;
			for (SmatBomDat bom : bomList) {
				if (StrUtil.equals(alt.getMainMatCode(), bom.getMatCode())) {
					bomList.remove(bom);
					bomDat = bom;
					break;
				}
			}

			if (bomDat == null) {//在BOM中找不到主料
				throw new BizException("SMAT-0003", new FieldMap("matCode", alt.getMatCode()), new FieldMap("mainMatCode",
						alt.getMainMatCode()), new FieldMap("prodCode", alt.getProdCode()));
			}
			alt.setMainMatCode(bomDat.getMatCode());
			alt.setMainMatVer(bomDat.getMatVer());
			alt.setUnit(altMat.getMatUnit());
			bomDat.setAltFlag('Y');
			upBomList.add(bomDat);
		}

		for (SmatBomDat bom : bomList) {
			bom.setAltFlag('N');
			upBomList.add(bom);
		}

		updateBatch(upBomList, "altFlag");
		if (!CollUtil.isEmpty(oldAltList)) {
			deleteBatch(oldAltList);
		}
		insertBatch(in.getAltList());

		SmatSaveProductionManufactureAlternateMaterialOut out = new SmatSaveProductionManufactureAlternateMaterialOut();

		return out;
	}
}